
require('./common');

exports.load = function(req, res){
	var strSQL = "SELECT ID, FormName, Name FROM tblworkspace WHERE FormName <> '' ORDER BY Name ";
	var onClick ='';
				
	conn.query(strSQL, function(err, results, fields){	
		if(err) throw err;
		
		var index=0;
		var output='';
		
		loadFormswithNotification(index, results, output, req, res);
	
	});	
}


function loadFormswithNotification(index, results, output, request, response){	//load company forms and notifications
	var onClick =''; //link of the form view
	var count = results.length;//result number of rows
	
	onClick = "getRequests('" + results[index].ID +"','" + results[index].Name +"','0', '" + results[index].FormName + "')";//set onclick event
									
	output += '<div class="forms" onclick="' + onClick +';return false;">';			
	output += '<div style="float: left;"><a onclick="' + onClick +';return false;" style="text-decoration:none;color:#444"><img src="/images/Posts.png" style="vertical-align:middle">&nbsp;' + results[index].FormName + '</a></div>';
	
	
	var strSQL = 'SELECT COUNT(*) AS NUMREC FROM ' + results[index].Name + " WHERE UnRead= '1' " ;
	conn.query(strSQL, function(err, res, fields){
		if(res[0].NUMREC!=0){
			output +='<div class="notification">' + res[0].NUMREC + '</div>';
		}
		output+= '<div style="clear: both; "></div>';	
		output+= '</div><br/>';	
		
		index++;
		
		if(index<count){
			loadFormswithNotification(index, results, output, request, response)
		}else{
			response.send(output);
		}	
	});
}

